package com.hua.base;

public class ShallowCloneDemo implements Cloneable{
    @Override //重写了clone方法
    protected ShallowCloneDemo clone() throws CloneNotSupportedException {
        return (ShallowCloneDemo) super.clone();
    }
    private int[] arr;
    public ShallowCloneDemo(){ //无参构造方法
        arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
    }
    public void set(int index,int value){
        arr[index] = value;
    }
    public int get(int index){
        return arr[index];
    }


    public static void main(String[] args) {
        ShallowCloneDemo e1 = new ShallowCloneDemo();
        ShallowCloneDemo e2 = null;  //e2的对象为空
        try {
            e2 = e1.clone(); //e1给e2浅拷贝了一个对象
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        e1.set(2,222);//在e1这个对象，数组赋值222， e2这个对象就会接受到222的值
        System.out.println(e2.get(2));


    }
}
